package cn.lili.controller.feign;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.modules.goods.entity.dto.GoodsSkuDTO;
import cn.lili.modules.promotion.client.PromotionsClient;
import cn.lili.modules.promotion.entity.dos.Coupon;
import cn.lili.modules.promotion.entity.dos.FullDiscount;
import cn.lili.modules.promotion.entity.dos.MemberCoupon;
import cn.lili.modules.promotion.entity.dos.PromotionGoods;
import cn.lili.modules.promotion.entity.dto.search.KanjiaActivitySearchParams;
import cn.lili.modules.promotion.entity.dto.search.MemberCouponSearchParams;
import cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams;
import cn.lili.modules.promotion.entity.vos.PintuanMemberVO;
import cn.lili.modules.promotion.entity.vos.PintuanVO;
import cn.lili.modules.promotion.entity.vos.PointsGoodsVO;
import cn.lili.modules.promotion.entity.vos.RedPackGoodsVO;
import cn.lili.modules.promotion.entity.vos.kanjia.KanjiaActivityVO;
import cn.lili.modules.promotion.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author paulG
 * @since 2021/12/28
 **/
@RestController
@RequiredArgsConstructor
public class PromotionsFeignController implements PromotionsClient {

    private final MemberCouponService memberCouponService;

    private final PintuanService pintuanService;

    private final KanjiaActivityService kanjiaActivityService;

    private final PointsGoodsService pointsGoodsService;

    private final PromotionGoodsService promotionGoodsService;

    private final PromotionService promotionService;

    private final CouponService couponService;

    private final FullDiscountService fullDiscountService;

    private final KanjiaActivityGoodsService kanjiaActivityGoodsService;

    private final RedPackGoodsService redPackGoodsService;

    /**
     * 根据当前会员有效的优惠券
     *
     * @return 当前促销活动集合
     */
    @Override
    public List<MemberCoupon> getMemberCoupons(MemberCouponSearchParams param) {
        return memberCouponService.getMemberCoupons(param);
    }

    @Override
    public FullDiscount getFullDiscount(String id) {
        return fullDiscountService.getById(id);
    }

    /**
     * 获取当前用户的优惠券列表（优先读取缓存）
     *
     * @param memberId 会员id
     * @return 会员优惠券列表
     */
    @Override
    public List<MemberCoupon> getMemberCoupons(String memberId) {
        return memberCouponService.getMemberCoupons(memberId);
    }

    /**
     * 获取当前会员全品类优惠券
     *
     * @param memberId 会员Id
     * @param storeId  店铺id
     * @return 会员优惠券列表
     */
    @Override
    public List<MemberCoupon> getAllScopeMemberCoupon(String memberId, List<String> storeId) {
        return memberCouponService.getAllScopeMemberCoupon(memberId, storeId);
    }

    @Override
    public PintuanVO getPintuanVO(String id) {
        return pintuanService.getPintuanVO(id);
    }

    @Override
    public KanjiaActivityVO getKanjiaActivityVO(KanjiaActivitySearchParams kanjiaActivitySearchParams) {
        return kanjiaActivityService.getKanjiaActivityVO(kanjiaActivitySearchParams);
    }

    @Override
    public List<MemberCoupon> getCurrentGoodsCanUse(String memberId, List<String> couponIds, Double totalPrice) {
        return memberCouponService.getCurrentGoodsCanUse(memberId, couponIds, totalPrice);
    }

    /**
     * 获取促销商品信息
     *
     * @param searchParams 查询参数
     * @return 促销商品列表
     */
    @Override
    public List<PromotionGoods> listFindAll(PromotionGoodsSearchParams searchParams) {
        return promotionGoodsService.listFindAll(searchParams);
    }

    /**
     * 获取促销商品信息
     *
     * @param searchParams 查询参数
     * @return 促销商品信息
     */
    @Override
    public PromotionGoods getPromotionsGoods(PromotionGoodsSearchParams searchParams) {
        return promotionGoodsService.getPromotionsGoods(searchParams);
    }

    /**
     * 获取会员优惠券
     *
     * @param param 查询参数
     * @return 会员优惠券列表
     */
    @Override
    public MemberCoupon getMemberCoupon(MemberCouponSearchParams param) {
        return memberCouponService.getMemberCoupon(param);
    }

    /**
     * 根据商品索引获取当前商品索引的所有促销活动信息
     *
     * @param storeId    店铺id
     * @param goodsSkuId 商品skuId
     * @return 当前促销活动集合
     */
    @Override
    public Map<String, Object> getGoodsSkuPromotionMap(String storeId, String goodsSkuId) {
        return promotionService.getGoodsSkuPromotionMap(storeId, goodsSkuId);
    }

    /**
     * 根据ID获取积分详情
     *
     * @param skuId 商品SkuId
     * @return 积分详情
     */
    @Override
    public PointsGoodsVO getPointsGoodsDetailBySkuId(String skuId) {
        return pointsGoodsService.getPointsGoodsDetailBySkuId(skuId);
    }

    /**
     * 使用优惠券
     *
     * @param couponId 优惠券id
     * @param usedNum  使用数量
     */
    @Override
    public void usedCoupon(String couponId, Integer usedNum) {
        couponService.usedCoupon(couponId, usedNum);
    }

    @Override
    public void removeByGoodsIds(String goodsIdsJsonStr) {
        if (CharSequenceUtil.isEmpty(goodsIdsJsonStr)) {
            return;
        }
        List<String> goodsIds = JSONUtil.toList(goodsIdsJsonStr, String.class);
        promotionGoodsService.deletePromotionGoodsByGoods(goodsIds);
        kanjiaActivityGoodsService.deleteByGoodsIds(goodsIds);
    }

    @Override
    public List<PromotionGoods> findSkuValidPromotions(List<GoodsSkuDTO> skus) {
        return promotionGoodsService.findSkuValidPromotions(skus);
    }

    @Override
    public Map<String, Object> wrapperPromotionMapList(List<PromotionGoods> promotionGoodsList) {
        return promotionService.wrapperPromotionMapList(promotionGoodsList);
    }

    @Override
    public Coupon getCouponById(String id) {
        return couponService.getById(id);
    }

    public List<PintuanMemberVO> getPintuanMember(String pintuanId, String skuId) {
        return pintuanService.getPintuanMember(pintuanId, skuId);
    }
}
